﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SWIRL
{
    //This class Detects Peaks
    public class PeakDetection
    {
        // string axisIndex; //this variable is using for defining indexer of axix array

        public List<DetectedPeaks> DetectPeaks(AccelerometerDataArray adArray, Axis axis, PeaksType peaksType)
        {
            //return array initialization where detectedPeaks - array with detected peaks
            List<DetectedPeaks> detectedPeaks = new List<DetectedPeaks>();
            DetectedPeaks dp = new DetectedPeaks();

            List<TimeAndAxisValue> data = new List<TimeAndAxisValue>();


            //this is data with dateTime stamp and selected axis values
            data = adArray.GetAccelerometerData(axis);

            //List for Emission Peaks
            List<DetectedPeaks> detectedEmiPeaks = new List<DetectedPeaks>();
            //List for Absoption peaks
            List<DetectedPeaks> detectedAbsPeaks = new List<DetectedPeaks>();



            //   double data; // the data - data[1]

            int data_count = data.Count(); // row count of data


            int emi_peaks; // emission peaks will be put here - emi_peaks
            int num_emi_peaks; // number of emission peaks found - &emi_count
            int max_emi_peaks; // maximum number of emission peaks - MAX_PEAK
            int absop_peaks; // absorption peaks will be put here - absorp_peaks
            int num_absop_peaks; // number of absorption peaks found - &absorp_count
            int max_absop_peaks; // maximum number of absorption peaks - MAX_PEAK
            double delta = 1.3; // delta used for distinguishing peaks - delta - ASSIGNED 1 FOR TEST
            int emi_first = 1; // should we search emission peak first of absorption peak first? - emission_first



            int i;
            double mx;
            double mn;
            int mx_pos = 0;
            int mn_pos = 0;

            int is_detecting_emi = emi_first;


            mx = data[0].axisValue;
            mn = data[0].axisValue;

            num_emi_peaks = 0;
            num_absop_peaks = 0;

            for (i = 1; i < data_count; ++i)
            {
                if (data[i].axisValue > mx)
                {
                    mx_pos = i;
                    mx = data[i].axisValue;
                }
                if (data[i].axisValue < mn)
                {
                    mn_pos = i;
                    mn = data[i].axisValue;
                }


                //if (is_detecting_emi && data[i].axisValue < mx - delta)
                if ((is_detecting_emi == 1) && (data[i].axisValue < mx - delta))
                {
                    // if (num_emi_peaks >= max_emi_peaks) /* too many peaks*/
                    //    return 1;




                    //add structure to array of detected emission peaks 

                    dp.dateTime = data[i].dateTime;
                    dp.peakValue = data[i].axisValue;

                    detectedEmiPeaks.Add(dp);


                    //emi_peaks[*num_emi_peaks] = mx_pos;

                    ++(num_emi_peaks);
 
                    is_detecting_emi = 0;

                    i = mx_pos - 1;

                    mn = data[mx_pos].axisValue;
                    mn_pos = mx_pos;
                }

                //detection absoption

                else if ((is_detecting_emi == 0) && (data[i].axisValue > mn + delta))
                {

                    //    if (*num_absop_peaks >= max_absop_peaks) /*too many peaks*/
                    //        return 2;

                    //    absop_peaks[*num_absop_peaks] = mn_pos;

                    dp.dateTime = data[i].dateTime;
                    dp.peakValue = data[i].axisValue;

                    detectedAbsPeaks.Add(dp);

                    ++(num_absop_peaks);

                    is_detecting_emi = 1;

                    i = mn_pos - 1;

                    mx = data[mn_pos].axisValue;
                    mx_pos = mn_pos;
                }

            }


            return detectedEmiPeaks;
        }
    }
}
